package threadcoreknowledge.createthread.stopthread;

/**
 * 最佳实践1：
 * catch了InterruptedException之后的优先选择：在方法前面中抛出异常
 * 那么在run中就会强制try-catch
 */
public class RightWayStopThreadInProd implements Runnable {

    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(new RightWayStopThreadInProd());
        thread.start();
        Thread.sleep(1000);
        thread.interrupt();
    }

    @Override
    public void run() {
        while(true){
            System.out.println("go");
            try {
                throwInMethod();
            } catch (InterruptedException e) {

                //这里可以进行一系列操作
                System.out.println("保存日志");
                e.printStackTrace();
            }
        }
    }

    private void throwInMethod() throws InterruptedException {
        Thread.sleep(1000);
    }
}
